package com.roadjava.rbac.service.impl;
//服务层实现类，实现了RecommendationService接口，提供了具体的业务逻辑实现。

import com.roadjava.rbac.bean.Recommendation;
import com.roadjava.rbac.bean.dto.RecommendationDTO;
import com.roadjava.rbac.bean.vo.RecommendationVO;
import com.roadjava.rbac.mapper.RecommendationRepository;
import com.roadjava.rbac.service.RecommendationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RecommendationServiceImpl implements RecommendationService {

    private static final Logger logger = LoggerFactory.getLogger(RecommendationServiceImpl.class);
    @Autowired
    private RecommendationRepository recommendationRepository;

    @Override
    public List<Recommendation> getAllRecommendations() {
        logger.info("Fetching all recommendations");
        return recommendationRepository.findAll();
    }

    @Override
    public Recommendation saveRecommendation(Recommendation recommendation) {
        logger.info("Saving recommendation for user: {}", recommendation.getUserId());
        return recommendationRepository.save(recommendation);
    }

    @Override
    public Page<RecommendationVO> getRecommendations(Integer userId, Long resourceID, String userBehavior, Pageable pageable) {
        logger.info("Fetching recommendations with userId: {}, resourceID: {}, userBehavior: {}", userId, resourceID, userBehavior);
        return recommendationRepository.findRecommendations(userId, resourceID, userBehavior, pageable)
                .map(RecommendationVO::new);
    }

    @Override
    public Optional<RecommendationVO> getRecommendationById(Long id) {
        logger.info("Fetching recommendation by ID: {}", id);
        return recommendationRepository.findById(id)
                .map(RecommendationVO::new);
    }

    @Override
    public RecommendationVO saveRecommendation(RecommendationDTO recommendationDTO) {
        logger.info("Creating recommendation from DTO for user: {}", recommendationDTO.getUserId());
        Recommendation recommendation = new Recommendation(recommendationDTO);
        return new RecommendationVO(recommendationRepository.save(recommendation));
    }

    @Override
    public Optional<RecommendationVO> updateRecommendation(Long id, RecommendationDTO recommendationDTO) {
        logger.info("Updating recommendation with ID: {}", id);
        return recommendationRepository.findById(id)
                .map(recommendation -> {
                    recommendation.updateFromDTO(recommendationDTO);
                    return new RecommendationVO(recommendationRepository.save(recommendation));
                });
    }

    @Override
    public boolean deleteRecommendation(Long id) {
        logger.info("Deleting recommendation with ID: {}", id);
        if (recommendationRepository.existsById(id)) {
            recommendationRepository.deleteById(id);
            logger.info("Recommendation with ID: {} deleted successfully", id);
            return true; // 删除成功
        }
        logger.warn("Recommendation with ID: {} not found for deletion", id);
        return false; // ID 不存在，删除失败
    }
}